#include <windows.h>
/* Copy input to Windows clipboard
* Data lines must be terminated by the CR LF pair (0xD,0xA)
* data in: line1CRLFline2CRLFline3CRLF --- Caller must format
* "this is a line\n" is not acceptable,
* "this is a line\r\n" is acceptable.
* If clipboard data shows square empty boxes at line ends in Windows,
* it is because lines are terminated by \n only.
*/
int clipput(char *toclipdata)
{
char far *buffer;
int bytes;
HGLOBAL clipbuffer;
// Set data buffer length to accomodate toclipdata
bytes = strlen(toclipdata);
// Peform the transfer to clipboard magic
OpenClipboard(NULL);
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE,bytes+1);
buffer = (char far*)GlobalLock(clipbuffer);
if (buffer == NULL)
return GetLastError() * -1; // Do what you want to signal error
strcpy(buffer,toclipdata);
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
// Return byte count
return bytes; // non-negative value is success.
}